package com.example.nyf.mapper;

import com.example.nyf.dto.SysRoleDTO;
import com.example.nyf.entity.SysRole;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Set;

public interface SysRoleMapper {
    @Select("select id, role_name, role_key, status, remark, add_user_id, add_user_name, add_time from sys_role")
    public List<SysRole> getSysRoleList();

    @Select("select distinct a.perms from sys_menu a " +
            "left outer join sys_role_menu d on a.id = d.menu_id  " +
            "left outer join sys_user_role b on d.role_id=b.role_id  " +
            "where b.user_id = #{user_id} ")
    public Set<String> getSysPerByUserId(@Param("user_id") Integer user_id);

    @Select("select a.id, a.role_name, a.role_key, a.status, a.remark, a.add_user_id, a.add_user_name, a.add_time  from sys_role a " +
            "left outer join sys_user_role b on a.id=b.role_id  " +
            "where b.user_id = #{user_id} ")
    public Set<String> getSysRoleByUserId(@Param("user_id") Integer user_id);

    @Select("select * from sys_role where id = #{id}")
    public SysRole getSysRoleById(@Param("id") Integer id);

    @Select("select a.*, GROUP_CONCAT(c.id) as menu_id from sys_role a left outer join sys_role_menu b on a.id=b.role_id left outer join sys_menu c " +
            "on b.menu_id=c.id where a.id = #{role_id} ")
    public SysRoleDTO getRoleMenuById(@Param("role_id") Integer role_id);

    @Update("update sys_role set role_name = #{role_name}, remark = #{remark} where id = #{id}")
    public int updateSysRole(SysRoleDTO sysRoleDTO);

    @Insert("insert into sys_role(role_name, remark, add_user_id, add_user_name, add_time) values(#{role_name}, #{remark}" +
            ", #{add_user_id}, #{add_user_name}, #{add_time})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public int insertSysRole(SysRoleDTO sysRoleDTO);

    @Delete("delete from sys_role where id = #{id}")
    public int deleteSysRole(Integer id);
}
